#!/bin/sh
#
# Script takes folder(s) as input
#
# Example : ./generateLists.sh 2005-05*
#

#Prerequisites:
#
#find /storage01/papaya/raw/ab1 -name "*.ab1" | sed 's/.*\///' | sed 's/\.ab1//' | sort > all.sort.list

rm -f new.sort.list;
touch new.sort.list;
echo
echo "Removing old files to copy"
rm -rf files_to_copy;

for i in $@; do

    PROBLEM="";    

    echo "---------------------------"
    echo "Checking $i"

    if [ -d $i ]; then

	mkdir $i/lists

	grep \! $i/output.txt > $i/lists/problem.grep.java
	if [ -s $i/lists/problem.grep.java ]; then
	    echo "!!! $i had a problem with previous java application. See $i/lists/problem.grep.java"
	    PROBLEM="$PROBLEM'$i had a problem with previous java application' "
	else
	    echo "Java Seq. Qual ok";
	fi

	find $i/processed -name "*.fasta" > $i/lists/fasta.full_path.list;
	find $i/processed -name "*.qual" > $i/lists/qual.full_path.list;

	find $i/raw -name "*.ab1" > $i/lists/ab1.full_path.list;
	sed 's/.*\///' $i/lists/ab1.full_path.list | sed 's/\.ab1//' | sort > $i/lists/ab1.sort.list;
	NUM_ABI=`cat $i/lists/ab1.sort.list | wc -l`;
	echo "found $NUM_ABI ab1 files";

	find $i/raw -name "*.phd.1" > $i/lists/phd.full_path.list;
	sed 's/.*\///' $i/lists/phd.full_path.list | sed 's/\.phd\.1//' | sort > $i/lists/phd.sort.list;
	NUM_PHD=`cat $i/lists/phd.sort.list | wc -l`;
	echo "found $NUM_PHD phd files";


	diff $i/lists/ab1.sort.list $i/lists/phd.sort.list > $i/lists/problem.ab1-vs-phd.list
	if [ -s $i/lists/problem.ab1-vs-phd.list ]; then
	    echo "!!! $i does not have the same number of ab1 and phd files. See $i/lists/problem.ab1-vs-phd.list"
	    PROBLEM="$PROBLEM'$i does not have the same number of ab1 and phd files...' "
	else
	    echo "ab1 files = phd files";
	fi


	if [ -s $i/lists/ab1.sort.list ]; then

            grep -r "N" $i/processed/*.fasta | \
                grep -v ">" | \
                egrep -v "$i\/processed\/.*\.fasta\:\<N*\>" | \
                sed 's/:.*$//' | \
                uniq > $i/lists/problem.grep.Ns;

            if [ -s $i/lists/problem.grep.Ns ]; then
                echo "!!! $i probably called Ns. See $i/lists/problem.grep.Ns"
                PROBLEM="$PROBLEM'$i probably called Ns' "
            fi
	    
            egrep -v ^P[369]K-[0-9][0-9][0-9][0-9][0-9]-[A-P][0-2][0-9]\.[FR]$ $i/lists/ab1.sort.list > $i/lists/problem.ab1.badnames.list
	    if [ -s $i/lists/problem.ab1.badnames.list ]; then
		echo "!!! $i has ab1 bad names. See $i/lists/problem.ab1.badnames.list";
		PROBLEM="$PROBLEM'$i has ab1 bad names' ";
	    else 
		echo "ab1 names ok";
	    fi
	    egrep -v ^P[369]K-[0-9][0-9][0-9][0-9][0-9]-[A-P][0-2][0-9]\.[FR]$ $i/lists/phd.sort.list > $i/lists/problem.phd.badnames.list
	    if [ -s $i/lists/problem.phd.badnames.list ]; then
		echo "!!! $i has phd bad names. See $i/lists/problem.phd.badnames.list";
		PROBLEM="$PROBLEM'$i has phd bad names' ";
	    else 
		echo "phd names ok";
	    fi

	    cat all.sort.list $i/lists/ab1.sort.list new.sort.list | sort | uniq -d > $i/lists/problem.ab1.DUPS.list;
	    if [ -s $i/lists/problem.ab1.DUPS.list ]; then
		echo "!!! $i has ab1 dups. See $i/lists/problem.ab1.DUPS.list"
		PROBLEM="$PROBLEM\$i has ab1 dups' ";
	    else 
		echo "No ab1 dups";
	    fi
	    cat all.sort.list $i/lists/phd.sort.list new.sort.list | sort | uniq -d > $i/lists/problem.phd.DUPS.list;
	    if [ -s $i/lists/problem.phd.DUPS.list ]; then
		echo "!!! $i has phd dups. See $i/lists/problem.phd.DUPS.list";
		PROBLEM="$PROBLEM'$i has phd dups' ";
	    else 
		echo "No phd dups";
	    fi
	else 
	    PROBLEM="$PROBLEM'$i empty' ";
	fi        

	if [ -z "$PROBLEM" ]; then
	    echo "Copying files from $i";

	    ./makeNewRawFilesList.perl < $i/lists/ab1.full_path.list > $i/lists/files_to_copy.list;
	    ./makeNewRawFilesList.perl < $i/lists/phd.full_path.list >> $i/lists/files_to_copy.list;
	    ./makeNewProcessedFilesList.perl < $i/lists/fasta.full_path.list >> $i/lists/files_to_copy.list;
	    ./makeNewProcessedFilesList.perl < $i/lists/qual.full_path.list >> $i/lists/files_to_copy.list;
	    
            /bioinfo/soft/packages/cmhtools/bin/smp-runner --smp=16 --command='cp <1> <2>' < $i/lists/files_to_copy.list > /dev/null 
	    
            cat $i/lists/ab1.sort.list >> new.sort.list

	else
	    echo "!!! Skipping because $PROBLEM";
	fi

    else
	echo "!!! $i not a directory"
    fi
done

echo "---------------------------"
echo
